package com.clss.commandhelper.function.newEvidence;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clss.commandhelper.R;
import com.clss.commandhelper.view.CountDownTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/1.
 */

public class AudioTaskAdapter extends BaseQuickAdapter<TaskPoliceFile, BaseViewHolder> {
    Context context;
    List<TaskPoliceFile> data;
    int max = 9;
    private List<Integer> itemState;
    public final static int stop = 0;
    public final static int start = 1;

    public void setMax(int max) {
        this.max = max;
    }

    public AudioTaskAdapter(Context context, @Nullable List<TaskPoliceFile> data) {
        super(R.layout.item_audio, data);
        this.context = context;
        this.data = data;
        itemState = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, TaskPoliceFile item) {
        int layoutPosition = helper.getLayoutPosition();
//        if (data.size() < max && data.size() == layoutPosition) {
//            helper.setVisible(R.id.item_audio_countdown, false);
//            helper.setVisible(R.id.item_audio_del, false);
//            helper.setVisible(R.id.item_audio_img, false);
//            helper.setVisible(R.id.item_audio_recordbutton, true);
//            helper.addOnClickListener(R.id.item_audio_recordbutton);
//            return;
//        }

//        helper.setVisible(R.id.item_audio_countdown, true);
//        helper.setVisible(R.id.item_audio_del, true);
//        helper.setVisible(R.id.item_audio_img, true);
//        helper.setVisible(R.id.item_audio_recordbutton, false);
        CountDownTextView timeText = helper.getView(R.id.item_audio_countdown);
        timeText.setType(1);
        if (item.getRecordTimeLength() == 0 ){
            timeText.setVisibility(View.GONE);
        } else{
            timeText.setVisibility(View.VISIBLE);
        }
        timeText.setTime(item.getRecordTimeLength());

        if (itemState.size() == data.size()){
            if (itemState.get(helper.getLayoutPosition()) == start){
                helper.setImageResource(R.id.item_audio_img,R.drawable.voice_play);
                ImageView imageView = helper.getView(R.id.item_audio_img);
                AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
                animationDrawable.start();

                timeText.beginRun();
            }else
            if (itemState.get(helper.getAdapterPosition()) == stop){
                helper.setImageResource(R.id.item_audio_img,R.drawable.voice3);
                timeText.stopRun();
            }
        }else {
            helper.setImageResource(R.id.item_audio_img,R.drawable.voice3);
            itemState.add(stop);
        }
        helper.addOnClickListener(R.id.item_audio_del);

    }

//    @Override
//    public int getItemCount() {
//        if (data.size() < max)
//            return data.size() + 1;
//        else return data.size();
//    }

    public List<Integer> getItemState(){

        return itemState;
    }
    public void removeItemState(int position){
        if (itemState.size() > position){
            itemState.remove(position);
            notifyDataSetChanged();
        }
    }
    public void setItemState(int position,int value){
        itemState.set(position,value);
        notifyItemChanged(position);
//        notifyDataSetChanged();
    }
    public void setStartToStop(){
        for (int i = 0; i < itemState.size(); i++) {
            if (itemState.get(i) == start){
                itemState.set(i,stop);
                notifyItemChanged(i);
            }
        }
    }

}

